Moved all code into functions for module