Fixing some bugs and reducing the number of calls into Qt