Major refactoring to simplify the code and make objects more generic.