much updated style and simplification of functions