Code refactoring, cleanup and improvements. Rails 4.2 support

Lot lot changes. Big code refactoring.