[Security] made code easier to understand, added some missing unit tests