Refactored slightly the code