Reformated the functionality to remove redundant code.