Cleaned up lots of the code