clean up code & refactor function