Refactored Vlad's code;