moved logic from views to controllers'