Cleaning up code and fixing bugs