Completed refactoring. Now nearly everything works as expected!