Major refactoring to be more Rails-like.