Fix map infinity loop and add type spec

In case end is greater than start or end is lower than next shift map ended up in infinite loop.