fixed some bugs and moved test code to test