Cleanups and refactor