Documentation added and code re-ordered