Bunch of bugs fixed and optimisations

Pre 1.4.0