Implementation of feature Request 1412 with Reduced Code duplication

On the basis of the previous feedbacks, i've implemented few changes. Please review it.

If everything's all right, then i will make another commit with the change log.

Thanks :)