added traps for interrupt and cleanup; some restructuring