Cleaned up the code so it is more self documenting