Improvements to clean up the code and make it more idiomatic