Fixes and extra tests

Fixed index out of range behaviour and added some extra tests.