partially rewritten, optimized base