And can you try to debug it a little? Put some DUMPs/LOGs to get any clue about whether functions returns the right results etc?
(I would o it here, but I would need to install japanesse keyboard a learn how to use it... With my knowledge of language, it would be a little bit difficult I am afraid).