escape charecters handling and minor refactoring