cleanup and refactor a little bit