Fixed few nasty bugs

Overall performance and stability should be improved