reorganized and simplified
fixed merge