Function refactored and more comments and documentation added.
Documentation improved.