Add procedures and tidy code