Code cleanup and refactoring for better overview