Fix variable naming, weird behaviour and tests