Code clean-up and some function/key renaming