Simplified code by extracting a few methods