Single thread solution