refactored manual-code and added tests