refactor and clean-up code