Now you have magic numbers 1,2,4,8, 16 being anded with stageflags all through the code, but I have no idea what they mean. Please change them to named values.