Refactored view.py to make it a bit less monolithic