Refactor util functions. Add comments