Fix for issue #58, issue #83 and Issue #87

This resolves #58 and also resolves #83 and also resolves #87

Fix I came up for issue #58 is pretty similar to one submitted before. If you think it is not the right way to fix it, you can discard commits e506877 and 8266239.