Adjusted code to get rid of redundancy.