Refactor thanks to @shuhei's advice