Fixing branching mess up

This is code copied from master, that should be in dev branch