Cleanup IO, add docs, comments and move functions
Convert local function with receiver to function with parameter to make js compiler happy