Code cleanup + more tests