fix deprecation warnings and optimize