Rebuild and refactored general logic