Put code in functions, fixes #11