fixed #11 by moving things out into a function