Implemented the bulk of the functionality