Adding fix for #281 with tests, needs review

Ready for review