extend optional support. code restructured.