(#1) fixed some bugs and corner cases; ironed out logic