clean up and refactor a bit