Briefly refactored the main functions