Refactored, cleaned up code, and wrote better comments