Implemented the core logic