updated code with tests included