Refactored some portions to be more idiomatic in scala