Cleaned up functions, added comments