Complete code and text refactoring, to make it more universally applicable