Major refactoring/testing