Fixed bugs, cleaned up code.