some speed optimisations
put bugs fixed