Cleanup and refactoring logic out of the view