More tests and code cleanup