Cleaned up code, added comments