Big refactoring/cleanup