Refactoring and cleanups