Bugfixes and simplifications after revision
from bpinto. Thank you very much.