reworked the demorse function