Implemented almost all logic