Code optimization

Please, check how it works in ST2. In ST3 it works perfectly.