Add constants, rearrange code