Refactor and code clean up