Refactored to separate controller and added tests