Implemented correct sorting algorthm