Refactoring

Some cleanups and refactoring