Added test code, fixed some stupid bugs