Implemented the main functionality and headed off some bugs