Code clean-up

Simplification and reduction of the number of functions.