Modified for rails 4.0  Need tons of testing.  To be continued.