Corrected, refactored and added tests.