Remove test code and fix bugs