full rewrite using methods and simple data structures