Solution to PE#9. Will refactor later