Cleaned up the code to be easier to read