Refactored function definitions to be more idiomatic of javascript (I think?)