Added proper termination and finished initial implementation