misc changes to logic that hopefully make it more readable