optimisations and fixed tests
merged changes from master