refactor code to make sure everything works