hard refactoring, general fixes and creation of gem