Alternate fix to issue #5, some optimizations