WIP. Code cleanup. Added collapsed panel support.