Extended and cleaned up...still doesn't work correctly, though.