Fixed code and added tests