Code cleanup. Added more tests