Refactor and add Rails 3 support