Clean Julia code

Clean Julia code by encapsulating it in a function.