more concise version of Assignment3.1