Reduced code duplication and addded comments