Code clean up and refactoring