Shortened code for problem4