Some bugs fixed + tests