Rewrote the previous commit using a goto to avoid possible stack overflows

Signed-off-by: Toon Schoenmakers <nighteyes1993@gmail.com>