Implemented main loop