Refactored the code a bit so it makes more sense

Signed-off-by: Irmo van den Berge <bergerkiller@gmail.com>