Cleaned up some code, added some documentation