split up functionality and added tests