refactored apply and added tests