SQL queries from controller moved to model. Code not tested