Refactored entire codes