Fixed some errors, cleaned up code