fix bug with numeric sub key, refactor code