Reformatted code to make it easier to understand and expand on