fixed bugs; now works