Updated core functionality and comments, improved code flow.