Cleaned up controller code, added comments.