Major refactoring/cleanup