Refactoring of db functions into ActiveRecord, much better