code refactoring around sql stuff