refactor and some better testing