Working on improving algorithm