suggestion: use code like
if (!active) {
    return;
}

instead of
if (active) {
    // all the code
    // ....
}

You will have less indentation this way.