Refactor and cleanups