Fixed issue #16. Needs testing