Upgraded API. Much more readable code and some comments