fixed crash when device is rotating
little cleanup of code