Basic re-organisation

Constant extracted, variables moves, new functions