Implemented all of the main functionality.