Fix next/previous

When there was no next/previous elem, error was thrown.