modified given code to work with our logic