Fix some calculations
Fix segfault