Moved some logic from views to controller