Code cleanup, tests