Refactor railPosition(rails)

Should be faster as amount of rails increases and hopefully easier to
understand.
Add and organize tests