Improved performance, removed redundancy and passed function comment to PEP8 style.