Housecleaning + reorganize functions to a more logical order