Fixed bunch of problems, cleaned up some code