Fixed bug where one item was being skipped; cleaned up code