COMP: fixed bugs in code, now works mostly.