Clean up unused code, refactoring