Cleaned up code, added comments for readability