Optimization, refactoring, cleanup