Refactor - cleanup and reorder code