Region finding code update

Made code for finding regions smaller and easier to read
Region code now takes block to generate output so it can be re-used for multiple operations