Added test and adapted code based on digi604 's review