REFERENCE VERSION: Full functionality, and removed debugging shortcuts; this version should be used to produce results