Refactor code to be more idiomatic