flip flopped a and b and cleaned up the code a bit