more or less completed the assignment, with some bugs