cleaned code a bit and moved logic out of controller and to model