Fixed some bugs and general code cleanup...