hw-3: tidied up code