Cleaned up and commented lots of code