library codes refactored

I tried to : 

1 - remove *switch* and *if* statements as much as possible 
2- move related codes to separate functions

to give it better *performance* and *readability*.