Rewritten main script using separated functions. Added ns in function names.
Almost finished implementation of rewritten code + added first tests