Added algorithm for marking and Refactored the code.