Implemented most of the functionality