Pass test 9 also, refactor code