Refactor all SQL in Controller

Fix #3