Refactor and clean up of code