Updated code (refactored!)