Code sorting and some protection to errors