major refactoring of ruby code