Fixed #148 as suggested (need to do perf tests but should work correctly)