Working version with almost all of the requirements.