Tested and refactored