replace hacky function with jvm lib
simplify space piece calculation