Cleanup, fixing bugs, refactoring