Fixed bugs
Cleaned up the code