Main functionality. Needs testing and validations.