finished part (a), cleaned code