add check for optimal
remove print
improved with swap