A bit of refactoring; added testing