Cleaned up some code to make it more functional and readable