Refactored the cpde a bit