Make code cleaner and fix type error