Converted some literal sql into ORM