Move check for is_last before any action

Signed-off-by: Nikolay Nizov <nizovn@gmail.com>