bug fix and speed optimizations with relations

Signed-off-by: Andreas Zöllner <andi@go-for-fun.de>