removed repeated code and added tests