API code fixes and works more efficiently