fix numerous logic bugs; add sample input