Cleaned up code and added more documentation