used Kyle Meyer's suggestion from Stack Overflow
code cleanup
added switching branches, creating them when neccesary