unit tests and code cleanup

Added more unit tests and pruned dead code.