Refactored code, merged with Nicki