Tail recursive quick version by Mauhiz